package com.yukefms.engine.running;

/**
 * @Description Running event is designed to reply running situations to client. We design 8 events for 4 rule states,i.e.,
 *    <ul>
 *        <li>inactived event: INACTIVE STATE</li>
 *        <li>actived event: ACTIVED STATE</li>
 *        <li>matched event: PartialSuccess State </li>
 *        <li>success event: SUCCESS state</li>
 *        <li>time out event: FAILURE STATE</li>
 *        <li>try-time out event: FAILURE STATE</li>
 *        <li>unmatched event: FAILURE STATE</li>
 *        <li>input-error event: FAILURE STATE</li>
 *
 *    </ul>
 * @Author Ping
 * @Date 2021/6/22 0:07
 **/
public class RunningEvent {
    /**
     * types for running event
     */
    private EventCode eventCode ;

    private String ruleName ;

    private String currentAction ;

    private String content ;

    private long timeStamp ;

    private double currentScore ;

    private int trialNum ;

    public RunningEvent(){
        this.timeStamp = System.currentTimeMillis() ;
    }

    public RunningEvent(EventCode eventCode, String ruleName, String content) {
        this.eventCode = eventCode;
        this.ruleName = ruleName;
        this.content = content;
        this.timeStamp = System.currentTimeMillis();
        this.currentScore = 0 ;
    }

    public RunningEvent(EventCode eventCode, String ruleName, String currentAction, String content, double currentScore) {
        this.eventCode = eventCode;
        this.ruleName = ruleName;
        this.currentAction = currentAction;
        this.content = content;
        this.currentScore = currentScore;
        this.timeStamp = System.currentTimeMillis() ;
    }

    public void setCurrentScore(double currentScore) {
        this.currentScore = currentScore;
    }

    /**
     * get rule name
     * @return
     */
    public String relatedRule() {
        return this.ruleName ;
    }

    public String getContent() {
        return this.content ;
    }

    /**
     * NOTE that, the event string defined below is the information replied and show
     * to the client.
     * the event string format is defined as "EVENT_CDOE#RULENAME#CONTENT"
     * @return
     */
    @Override
    public String toString() {
        return  eventCode +
                "#" + ruleName +
                "#" + currentAction +
                "#" + content +
                "#" + currentScore +
                "#" + trialNum ;
    }


    public void setEventCode(EventCode eventCode) {
        this.eventCode = eventCode;
    }

    public void setRuleName(String ruleName) {
        this.ruleName = ruleName;
    }

    public void setContent(String content) {
        this.content = content;
    }

    public void setCurrentAction(String currentAction) {
        this.currentAction = currentAction;
    }

    public void setTrialNum(int trialNum) {
        this.trialNum = trialNum;
    }

    public EventCode getEventCode() {
        return eventCode;
    }

    public static class EventContent {
        /**
         *  <li>inactived event: INACTIVE STATE</li>
         *  *        <li>actived event: ACTIVED STATE</li>
         *  *        <li>matched event: PartialSuccess State </li>
         *  *        <li>success event: SUCCESS state</li>
         *  *        <li>time out event: FAILURE STATE</li>
         *  *        <li>trial out event: FAILURE STATE</li>
         *  *        <li>unmatched event: FAILURE STATE</li>
         *  *        <li>input-error event: FAILURE STATE</li>
         */
        public static final String FAILED_CONT = "SORRY YOU FAILED" ;
        public static final String SUCCESS_CONT = "CONGRATS YOU PASSED" ;
        public static final String INACTIVE_CONT = "NOT ACTIVIATED" ;
        public static final String ACTIVE_CONT = "PERFORMING GOOD" ;
        public static final String PMATCHED_CONT = "PATIALLY INCORRECT" ;
        public static final String TIMEOUT_CONT = "TIME OUT" ;
        public static final String TRIALOUT_CONT = "TRIAL OUT";
        public static final String INPUTERROR_CONT = "AJUST CAMERA DISTANCE" ;
        public static final String UNMATCHED_CONT = "UNMATCHED ACTION" ;
        public static final String ALL_FINISHED_CONT = "ALL ACTIONS FINSIHED" ;
        public static final String VIOLATED_SUFFIX = "YOU VIOLATED: " ;
        public static final String NOT_PERFORM_SUFFIX = "YOU FAILED PERFORM: " ;


    }

    /**
     * simple factory for building various events.
     * @param code
     * @param ruleName
     * @return
     */
    public static RunningEvent buildEvent(EventCode code, String ruleName) {
        return buildEvent(code, ruleName, "--", 0.0) ;

    }

    public static RunningEvent buildEvent(EventCode code, String ruleName, String currentAction, double currentScore) {
        RunningEvent event = new RunningEvent() ;
        event.setRuleName(ruleName);
        event.setCurrentAction(currentAction);
        event.setCurrentScore(currentScore);
        switch (code) {
            case FAILED:
                event.setEventCode(EventCode.FAILED);
                event.setContent(EventContent.FAILED_CONT);
                break ;
            case SUCCESS:
                event.setEventCode(EventCode.SUCCESS);
                event.setContent(EventContent.SUCCESS_CONT);
                break;
            case INACTIVED:
                event.setEventCode(EventCode.INACTIVED);
                event.setContent(EventContent.INACTIVE_CONT);
                break ;
            case PARTIAL_MATCHED:
                event.setEventCode(EventCode.PARTIAL_MATCHED);
                event.setContent(EventContent.PMATCHED_CONT);
                break ;
            case ACTIVED:
                event.setEventCode(EventCode.ACTIVED);
                event.setContent(EventContent.ACTIVE_CONT);
                break ;
            case TIME_OUT:
                event.setEventCode(EventCode.TIME_OUT);
                event.setContent(EventContent.TIMEOUT_CONT);
                break ;
            case TRIAL_OUT:
                event.setEventCode(EventCode.TRIAL_OUT);
                event.setContent(EventContent.TRIALOUT_CONT);
                break ;
            case INPUT_ERROR:
                event.setEventCode(EventCode.INPUT_ERROR);
                event.setContent(EventContent.INPUTERROR_CONT);
                break ;
            case UNMATCHED:
                event.setEventCode(EventCode.UNMATCHED) ;
                event.setContent(EventContent.UNMATCHED_CONT);
                break ;
            case ALL_FINISHED:
                event.setEventCode(EventCode.ALL_FINISHED);
                event.setContent(EventContent.ALL_FINISHED_CONT) ;
                break ;
            default:
                throw new IllegalArgumentException("Event code is incorrected.") ;
        }
        return event ;
    }
}
